package com.dyp.picindbdemo.web.rest;

import com.dyp.picindbdemo.domain.Picture;
import com.dyp.picindbdemo.repository.PictureMapper;
import com.dyp.picindbdemo.util.HeaderUtil;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Controller
public class PictureResource {

    @Autowired
    PictureMapper pictureMapper;

    @PostMapping(value = "/picture/{name}", consumes = {MediaType.APPLICATION_OCTET_STREAM_VALUE})
    @ResponseBody
    public ResponseEntity<Map> createPictureByOcta(@RequestBody byte[] picData,
                                                   @PathVariable String name,
                                                   @RequestParam(required = false) String type) throws URISyntaxException, IOException {

        Picture picture2DB = new Picture();
        picture2DB.setName(name);
        picture2DB.setPicture(picData);
        pictureMapper.insert(picture2DB);

        Map<String, Integer> result = new HashMap<>();
        result.put("success",201);
        return ResponseEntity
                .created(new URI("/picture"))
                .headers(HeaderUtil.createEntityCreationAlert("dbtest", false, "picture", ""))
                .body(result);
    }

    @PostMapping(value = "/picture", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    @ResponseBody
    public ResponseEntity<Map> createPicture( @RequestPart MultipartFile picData) throws URISyntaxException, IOException {
        InputStream is = picData.getInputStream();

        byte[] pictureData = new byte[(int) picData.getSize()];
        int length = is.read(pictureData);
        Picture picture2DB = new Picture();
        picture2DB.setName(picData.getOriginalFilename());
        picture2DB.setPicture(pictureData);
        pictureMapper.insert(picture2DB);
        Map<String, Integer> result = new HashMap<>();
        result.put("success",201);
        return ResponseEntity
                .created(new URI("/picture"))
                .headers(HeaderUtil.createEntityCreationAlert("dbtest", false, "picture", ""))
                .body(result);
    }

    //curl -X POST "http://localhost:8080/picture-request-part"
    // -H  "accept: */*" -H  "Content-Type: multipart/form-data"
    // -F "description=abc" -F "picData=@http-method.png;type=image/png"
    @PostMapping(value = "/picture-request-part", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    @ResponseBody
    public ResponseEntity<Map> createPicture2(
            @RequestPart String description,
            @RequestPart MultipartFile picData) throws URISyntaxException, IOException {
        InputStream is = picData.getInputStream();

        byte[] pictureData = new byte[(int) picData.getSize()];
        int length = is.read(pictureData);
        Picture picture2DB = new Picture();
        picture2DB.setName(picData.getOriginalFilename());
        picture2DB.setPicture(pictureData);
        pictureMapper.insert(picture2DB);
        Map<String, Integer> result = new HashMap<>();
        result.put("success",201);
        return ResponseEntity
                .created(new URI("/picture"))
                .headers(HeaderUtil.createEntityCreationAlert("dbtest", false, "picture", ""))
                .body(result);
    }

    //如果不带RequestPart,也没有MediaType.MULTIPART_FORM_DATA_VALUE标注，在swagger页面是不会显示picData参数
    //curl -X POST "http://localhost:8080/picture-without-request-part?description=sdfsfd"
    // -H  "accept: */*" -H  "Content-Type: multipart/form-data"
    // -F "picData=@Screenshot from 2022-09-15 17-29-04.png;type=image/png"
    @PostMapping(value = "/picture-without-request-part")
    @ResponseBody
    public ResponseEntity<Map> createPicture3(
            @RequestParam (value ="description",required = false)   String description,
            @RequestPart MultipartFile picData) throws URISyntaxException, IOException {
        InputStream is = picData.getInputStream();

        byte[] pictureData = new byte[(int) picData.getSize()];
        int length = is.read(pictureData);
        Picture picture2DB = new Picture();
        picture2DB.setName(picData.getOriginalFilename());
        picture2DB.setPicture(pictureData);
        pictureMapper.insert(picture2DB);
        Map<String, Integer> result = new HashMap<>();
        result.put("success",201);
        return ResponseEntity
                .created(new URI("/picture"))
                .headers(HeaderUtil.createEntityCreationAlert("dbtest", false, "picture", ""))
                .body(result);
    }

    @Operation(summary = "获取")
    @GetMapping("/picture/{name}")
    public ResponseEntity<byte[]> getPicturebyName(@PathVariable(value = "name")  String name){
        Picture picture = pictureMapper.selectByPrimaryKey(name);
        HttpHeaders headers = new HttpHeaders();
        SimpleDateFormat simp = new SimpleDateFormat("yyy-MM-dd");
        String times = simp.format(Calendar.getInstance().getTime());
//        headers.setContentDispositionFormData("attachment", "perpetual-"+ times+".jpeg");
        headers.setContentType(MediaType.IMAGE_JPEG);
        headers.setContentLength(picture.getPicture().length);
        return new ResponseEntity<byte[]>(picture.getPicture(),headers, HttpStatus.OK);
    }

}
